home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Sub Hunter264779152001.psc / Module1.bas < prev   
Encoding:
BASIC Source File  |  2001-09-15  |  17.6 KB  |  613 lines

  1. Attribute VB_Name = "Publics"
  2. Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  3. Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
  4. Declare Function GetPixel Lib "gdi32.dll" (ByVal hdc As Long, ByVal nXPos As Long, ByVal nYPos As Long) As Long
  5. Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
  6. Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
  7. Public Const SRCAND = &H8800C6
  8. Public Const SRCPAINT = &HEE0086
  9. Public Const SRCCOPY = &HCC0020
  10.  
  11.  
  12.  
  13. Public Type Coor
  14.  X As Integer
  15.  Y As Integer
  16.  Act As Boolean
  17.  Tag As Integer
  18. End Type
  19.  
  20. Public Type Player
  21.  X As Integer
  22.  Y As Integer
  23.  Ammo As Integer
  24.  Dire As Byte
  25.  Health As Integer
  26.  Score As Long
  27.  Speed As Currency
  28.  Firetime As Currency
  29.  Killed As Integer
  30. End Type
  31.  
  32. Public Type SubMarine
  33.  X As Integer
  34.  Y As Integer
  35.  Act As Boolean
  36.  Score As Integer
  37.  Dire As Integer
  38.  Speed As Integer
  39.  Damaged As Integer
  40. End Type
  41.  
  42. Public Type DropBombs
  43.  X As Integer
  44.  Y As Integer
  45.  Act As Boolean
  46.  Speed As Currency
  47. End Type
  48.  
  49. Public Type Bomber
  50.  X As Integer
  51.  Y As Integer
  52.  Act As Boolean
  53.  Dire As Integer
  54.  Speed As Integer
  55.  BombLoad As Integer
  56.  Droped As Boolean
  57. End Type
  58.  
  59. Public Type HighScore
  60.  PlName As String
  61.  plScore As Long
  62.  plDate As String
  63. End Type
  64.  
  65. Public Type PointsSign
  66.  Score As Integer
  67.  X As Integer
  68.  Y As Integer
  69.  Tag As Byte
  70. End Type
  71.  
  72. Public Const Bredde As Integer = 400
  73. Public Const Hoyde As Integer = 360
  74. Public Const ShipBredde = 53
  75. Public Const ShipHoyde = 14
  76. Public Const SubBredde = 36
  77. Public Const SubHoyde = 10
  78. Public Const PlaneBredde = 29
  79. Public Const PlaneHoyde = 12
  80. Public Const MaxAmmo = 7
  81. Public P1 As Player
  82. Public Shot(1 To 30) As Coor
  83. Public Subs(1 To 30) As SubMarine
  84. Public HighS(1 To 10) As HighScore
  85. Public Planes(1 To 10) As Bomber
  86. Public Bombs(1 To 30) As DropBombs
  87. Public Explo(1 To 10) As Coor
  88. Public Signs(1 To 10) As PointsSign
  89. Public TheKing As Coor
  90. Public NumPlanes As Integer
  91. Public NumSubs As Integer
  92. Public NumShots As Integer
  93. Public NumBombs As Integer
  94. Public DontClose As Boolean
  95. Public MainPause As Boolean
  96.  
  97.  
  98. Public Function PlaySound(File As String)
  99. Const SND_SYNC = &H0
  100. Const SND_ASYNC = &H1
  101. Const SND_NODEFAULT = &H2
  102. Const SND_LOOP = &H8
  103. Const SND_NOSTOP = &H10
  104.     wFlags% = SND_ASYNC Or SND_NODEFAULT
  105.     Svar = sndPlaySound(App.Path & "\" & File & ".wav", wFlags%) 'Send the sound to the big world
  106. End Function
  107.  
  108. Public Sub Fire() 'This is what happens when the player fires
  109. Dim A As Integer
  110.     
  111.     If (GetTickCount - P1.Firetime) < 300 Then Exit Sub 'A delay of 300 millisec between each shot
  112.     If P1.Ammo = 0 Then Exit Sub 'out of ammo, don't fire
  113.     If NumShots = 30 Then Exit Sub 'to many bombs active, don't fire
  114.     
  115.     P1.Firetime = GetTickCount 'set time till next fire
  116.     P1.Ammo = P1.Ammo - 1 'remove one in ammo
  117.     NumShots = NumShots + 1 'add a shot to the counting variable
  118.     
  119.     
  120.     A = 1
  121.     Do Until Not Shot(A).Act 'find an open shot slot
  122.         A = A + 1
  123.     Loop
  124.     
  125.     With Shot(A)
  126.         .Act = True 'atctivate it
  127.         .Y = P1.Y + ShipHoyde 'put it at the bottom of the ship
  128.         .X = P1.X + (ShipBredde / 2) 'an in the middle of it
  129.     End With
  130. End Sub
  131.  
  132. Public Sub MakeSub() 'Triggerd every tick, it makes subs
  133. Dim A As Integer
  134.     If NumSubs = 30 Then Exit Sub 'Too many subs are active
  135.     If Boss.Act Then Exit Sub 'Don't create subs when at the boss
  136.     
  137.     Randomize
  138.     temp = (Rnd * 130)
  139.     If temp < 2 + 30 - NumSubs Then 'if a small chance occures it creats a sub
  140.     
  141.         NumSubs = NumSubs + 1 'adds a sub to the subcounter
  142.         
  143.         A = 1
  144.         Do Until Not Subs(A).Act Or A = 30 'finds a free slot
  145.             A = A + 1
  146.         Loop
  147.         
  148.         With Subs(A)
  149.         .Act = True 'activates the sub
  150.         
  151.         If Int((Rnd * 2) + 1) = 1 Then 'put's it eighter at the left or right side of the screen
  152.             .X = 0 - SubBredde - 2
  153.             .Dire = 2
  154.         Else
  155.             .X = Bredde + 2
  156.             .Dire = 1
  157.         End If
  158.         
  159.         .Y = Int((Rnd * 200) + 130) 'Give a random Y point to enter
  160.         Randomize
  161.         temp = Int((Rnd * 100) + 1) 'Give a random speed
  162.         Select Case temp
  163.         Case 80 To 100
  164.             .Speed = 3
  165.         Case 50 To 80
  166.             .Speed = 2
  167.         Case Else
  168.             .Speed = 1
  169.         End Select
  170.         
  171.         
  172.         .Score = (.Speed * 2) * (.Y / 8) 'calculate score for killing sub using speed and depth
  173.         
  174.         End With
  175.     End If
  176. End Sub
  177. Public Sub Movesubs() 'moves all the subs
  178.     For A = 1 To 30
  179.     With Subs(A)
  180.         If .Act Then
  181.         'M is the speed at which the subs move
  182.         If .Dire = 2 Then M = .Speed 'going right
  183.         If .Dire = 1 Then M = -1 * .Speed 'going left
  184.         
  185.         If .Damaged <> 0 Then 'if it's hit and sinking:
  186.             .Damaged = .Damaged + 1
  187.             .Y = .Y + 3
  188.             If .Damaged = 10 Then 'sunk deep enough, deactivate
  189.                 .Damaged = 0
  190.                 .X = 0
  191.                 .Y = 0
  192.                 .Dire = 0
  193.                 .Act = False
  194.             End If
  195.         Else 'if not, move it
  196.             .X = .X + M
  197.         End If
  198.         'Reach end of screen?
  199.         If .X < 0 - SubBredde - 2 Or .X > Bredde + 2 Then
  200.             .Act = False
  201.             .Dire = 0
  202.             .Score = 0
  203.             .Speed = 0
  204.             .Damaged = 0
  205.             .X = 0
  206.             .Y = 0
  207.             NumSubs = NumSubs - 1
  208.         End If
  209.                     
  210.         End If
  211.     End With
  212.     Next A
  213. End Sub
  214. Public Sub MovePlanes()
  215.     For A = 1 To 10
  216.     With Planes(A)
  217.         If .Act Then
  218.         
  219.         If .Dire = 2 Then M = .Speed
  220.         If .Dire = 1 Then M = -1 * .Speed
  221.         
  222.         .X = .X + M
  223.         
  224.         'Reach the edge
  225.         If .X < 0 - PlaneBredde - 2 Or .X > Bredde + 2 Then
  226.             .Act = False
  227.             .Dire = 0
  228.             .Speed = 0
  229.             .Droped = False
  230.             .X = 0
  231.             .Y = 0
  232.             .BombLoad = 0
  233.             NumPlanes = NumPlanes - 1
  234.         End If
  235.                     
  236.         End If
  237.     End With
  238.     Next A
  239. End Sub
  240. Public Sub MoveShots()
  241.     For A = 1 To 30
  242.         With Shot(A)
  243.         If .Act Then
  244.             .Y = .Y + 1.8
  245.             
  246.             'Hit a sub?
  247.             HitSubCheck (A)
  248.             
  249.             If .Y >= Hoyde Then 'Reach the bottom?
  250.                 .Act = False
  251.                 .X = 0
  252.                 .Y = 0
  253.                 NumShots = NumShots - 1
  254.             End If
  255.         End If
  256.         End With
  257.         
  258.         
  259.         With Bombs(A) 'Bombs dropped from planes
  260.         If .Act Then
  261.             .Speed = .Speed - (.Speed * 0.1)
  262.             .X = .X + .Speed
  263.             .Y = .Y + 2
  264.             
  265.             HitShipCheck (A)
  266.             
  267.             If .Y >= 117 Then 'Reach surface, deactivate
  268.                 .Act = False
  269.                 .X = 0
  270.                 .Y = 0
  271.                 .Speed = 0
  272.                 NumBombs = NumBombs - 1
  273.             End If
  274.             
  275.         End If
  276.         End With
  277.     Next A
  278.     'The King
  279.     If TheKing.Act Then
  280.         TheKing.X = TheKing.X - 1
  281.         If TheKing.X <= -24 Then 'Deactivate
  282.             PlaySound "elvis"
  283.             TheKing.Act = False
  284.             TheKing.X = 0
  285.             TheKing.Y = 0
  286.             TheKing.Tag = 0
  287.         End If
  288.         If TheKing.Tag = 0 Then
  289.             TheKing.Tag = 2
  290.         Else: TheKing.Tag = TheKing.Tag - 1
  291.         End If
  292.         
  293.         If Rnd > 0.98 Then PlaySound "elvis2"
  294.     End If
  295. End Sub
  296.  
  297. Public Sub HitSubCheck(M)
  298. Dim Svar(1 To 4)
  299.     
  300.     Svar(1) = GetPixel(Form1.Pic2.hdc, Shot(M).X, Shot(M).Y)
  301.     Svar(2) = GetPixel(Form1.Pic2.hdc, Shot(M).X + 6, Shot(M).Y)
  302.     Svar(3) = GetPixel(Form1.Pic2.hdc, Shot(M).X + 6, Shot(M).Y + 6)
  303.     Svar(4) = GetPixel(Form1.Pic2.hdc, Shot(M).X, Shot(M).Y + 6)
  304.     For A = 1 To 4
  305.         If Svar(A) <> vbWhite And Not Svar(A) = -1 Then
  306.             'Check the boss
  307.             If Boss.Act Then
  308.                 Select Case A
  309.                 Case 1
  310.                     If Boss.X <= Shot(M).X And Boss.X + BossBredde >= Shot(M).X Then
  311.                         If Shot(M).Y >= Boss.Y And Shot(M).Y <= Boss.Y + BossHoyde Then
  312.                             Hitboss
  313.                         End If
  314.                     End If
  315.                 Case 2
  316.                     If Boss.X <= Shot(M).X + 6 And Boss.X + BossBredde >= Shot(M).X + 6 Then
  317.                         If Shot(M).Y >= Boss.Y And Shot(M).Y <= Boss.Y + BossHoyde Then
  318.                             Hitboss
  319.                         End If
  320.                     End If
  321.                 Case 3
  322.                     If Boss.X <= Shot(M).X + 6 And Boss.X + BossBredde >= Shot(M).X + 6 Then
  323.                         If Shot(M).Y + 6 >= Boss.Y And Shot(M).Y + 6 <= Boss.Y + BossHoyde Then
  324.                             Hitboss
  325.                         End If
  326.                     End If
  327.                 Case 4
  328.                     If Boss.X <= Shot(M).X And Boss.X + BossBredde >= Shot(M).X Then
  329.                         If Shot(M).Y + 6 >= Boss.Y And Shot(M).Y + 6 <= Boss.Y + BossHoyde Then
  330.                             Hitboss
  331.                         End If
  332.                     End If
  333.                 End Select
  334.             End If
  335.             'Now check the subs
  336.             For s = 1 To 30
  337.                 Select Case A
  338.                 Case 1
  339.                     If Subs(s).X <= Shot(M).X And Subs(s).X + SubBredde >= Shot(M).X Then
  340.                         If Shot(M).Y >= Subs(s).Y And Shot(M).Y <= Subs(s).Y + SubHoyde Then
  341.                             Killsub (s)
  342.                         End If
  343.                     End If
  344.                 Case 2
  345.                     If Subs(s).X <= Shot(M).X + 6 And Subs(s).X + SubBredde >= Shot(M).X + 6 Then
  346.                         If Shot(M).Y >= Subs(s).Y And Shot(M).Y <= Subs(s).Y + SubHoyde Then
  347.                             Killsub (s)
  348.                         End If
  349.                     End If
  350.                 Case 3
  351.                     If Subs(s).X <= Shot(M).X + 6 And Subs(s).X + SubBredde >= Shot(M).X + 6 Then
  352.                         If Shot(M).Y + 6 >= Subs(s).Y And Shot(M).Y + 6 <= Subs(s).Y + SubHoyde Then
  353.                             Killsub (s)
  354.                         End If
  355.                     End If
  356.                 Case 4
  357.                     If Subs(s).X <= Shot(M).X And Subs(s).X + SubBredde >= Shot(M).X Then
  358.                         If Shot(M).Y + 6 >= Subs(s).Y And Shot(M).Y + 6 <= Subs(s).Y + SubHoyde Then
  359.                             Killsub (s)
  360.                         End If
  361.                     End If
  362.                 End Select
  363.             Next s
  364.             If Shot(M).Act Then MakeExplo Shot(M).X, Shot(M).Y
  365.             Shot(M).Act = 0
  366.             Shot(M).X = 0
  367.             Shot(M).Y = 0
  368.             NumShots = NumShots - 1
  369.         End If
  370.     Next A
  371.     
  372. End Sub
  373.  
  374. Public Sub HitShipCheck(M)
  375. Dim Svar
  376.     
  377.     Svar = GetPixel(Form1.Pic2.hdc, Bombs(M).X, Bombs(M).Y)
  378.     
  379.     If Svar <> vbWhite Then
  380.         P1.Health = P1.Health - 1
  381.         With Bombs(M)
  382.         .Act = False
  383.         .Speed = 0
  384.         .X = 0
  385.         .Y = 0
  386.         End With
  387.         PlaySound "hit" 'play the sound
  388.         If P1.Health <= 0 Then
  389.             MainPause = True
  390.             P1.Health = 0
  391.             MsgBox "Game Over", vbOKOnly, Form1.Caption
  392.             Form1.PicExit_Click
  393.         End If
  394.     End If
  395. End Sub
  396. Public Sub Killsub(A)
  397.     With Subs(A)
  398.         If Not .Act Then Exit Sub
  399.         P1.Score = P1.Score + Subs(A).Score 'Add the subs score to the players
  400.         MakeSign Subs(A).Score, Subs(A).X, Subs(A).Y  'Make the Score Sign
  401.         .Score = 0
  402.         .Speed = 0
  403.         .Damaged = 1
  404.         P1.Killed = P1.Killed + 1 'Increase number of killed subs
  405.     End With
  406.     NumSubs = NumSubs - 1
  407.     OkToMakeBoss = True
  408. End Sub
  409. Sub MakeSign(Score, X, Y)
  410.     For A = 1 To UBound(Signs)
  411.         If Signs(A).Tag = 0 Then
  412.             Signs(A).Score = Score
  413.             Signs(A).Tag = 100
  414.             Signs(A).X = X + 10
  415.             Signs(A).Y = Y
  416.             Exit For
  417.         End If
  418.     Next A
  419. End Sub
  420.  
  421. Public Sub LoadScore()
  422.     Open App.Path & "\data.dat" For Random As #1 Len = 18
  423.     For A = 3 To 30 Step 3
  424.         Get #1, A - 2, HighS(A / 3).PlName
  425.         Get #1, A - 1, HighS(A / 3).plScore
  426.         Get #1, A, HighS(A / 3).plDate
  427.     Next A
  428.     Close #1
  429. End Sub
  430.  
  431. Public Sub SaveScore()
  432.     On Error Resume Next
  433.     Kill App.Path & "\data.dat"
  434.     Open App.Path & "\data.dat" For Random As #1 Len = 18
  435.     For A = 3 To 30 Step 3
  436.         Put #1, A - 2, HighS(A / 3).PlName
  437.         Put #1, A - 1, HighS(A / 3).plScore
  438.         Put #1, A, HighS(A / 3).plDate
  439.     Next A
  440.     Close #1
  441. End Sub
  442. Public Sub CheckKing()
  443.     If P1.X = 0 And GetAsyncKeyState(vbKeyE) And TheKing.Act = False Then
  444.         'Activate HIM
  445.         PlaySound "elvis2"
  446.         TheKing.Act = True
  447.         TheKing.Tag = 0
  448.         TheKing.X = Bredde + 1
  449.         TheKing.Y = Int((Rnd * 150) + 130)
  450.     End If
  451. End Sub
  452. Public Sub UpdateScore()
  453. Dim MyName As String
  454. Dim Score As Long
  455.     Score = P1.Score
  456.     
  457.     If Score = 0 Then Exit Sub
  458.     
  459.     For A = 1 To 10
  460.         If Score > HighS(A).plScore Then GoTo FantEn
  461.     Next A
  462.     ' No highscore, exit sub
  463.     Exit Sub
  464. FantEn:
  465.     
  466.     'Wanna save?
  467.     Svar = MsgBox("Congratulations! " & P1.Score & " points is a new highscore!" & vbNewLine & "Do you want to write it down?", vbYesNo, "New HighScore: " & A & ". place!")
  468.     If Svar = vbNo Then Exit Sub
  469.     
  470.     'Move previous scores down
  471.     For b = 10 To A + 1 Step -1
  472.         HighS(b).plDate = HighS(b - 1).plDate
  473.         HighS(b).PlName = HighS(b - 1).PlName
  474.         HighS(b).plScore = HighS(b - 1).plScore
  475.     Next b
  476.  
  477. NewName:
  478.     MyName = InputBox("Please input your name (Max 16 characters)", "New HighScore: " & A & ". place!")
  479.     If Len(MyName) > 16 Then GoTo NewName
  480.     If Len(MyName) = 0 Then GoTo NewName
  481.     
  482.     HighS(A).plDate = Date
  483.     HighS(A).PlName = MyName
  484.     HighS(A).plScore = P1.Score
  485.     frmHigh.Show , Form1
  486.     DontClose = True
  487. End Sub
  488.  
  489. Public Sub MakePlane()
  490.     If NumPlanes = 10 Then Exit Sub
  491.     
  492.     Randomize
  493.     temp = (Rnd * 130)
  494.     If temp < 20 Then
  495.     
  496.         NumPlanes = NumPlanes + 1
  497.         
  498.         A = 1
  499.         Do Until Not Planes(A).Act Or A = 10
  500.             A = A + 1
  501.         Loop
  502.         With Planes(A)
  503.         
  504.         .Act = True
  505.         
  506.         If Int((Rnd * 2) + 1) = 1 Then
  507.             .X = 0 - PlaneBredde - 2
  508.             .Dire = 2
  509.         Else
  510.             .X = Bredde + 2
  511.             .Dire = 1
  512.         End If
  513.         
  514.         .Y = Int((Rnd * 35) + 5)
  515.         
  516.         .Droped = False
  517.         .Speed = 4
  518.         
  519.         End With
  520.     End If
  521. End Sub
  522.  
  523. Public Sub DropBombs()
  524. Dim PL As Integer
  525.     For PL = 1 To 10
  526.         If Planes(PL).BombLoad > 0 Then GoTo AllClear
  527.         
  528.         If Planes(PL).Act And Not Planes(PL).Droped Then
  529.             
  530.             'Check if it is smart to drop bombs
  531.             If Planes(PL).X < P1.X + ShipBredde And Planes(PL).X > P1.X Then
  532.                 
  533.                 If Planes(PL).BombLoad = 0 Then
  534.                     Randomize
  535.                     Planes(PL).BombLoad = Int((Rnd * 7) + 3)
  536.                     Planes(PL).Droped = True
  537.                 End If
  538. AllClear:
  539.                 
  540.                 If NumBombs = 30 Then Exit Sub
  541.  
  542.                 NumBombs = NumBombs + 1
  543.                 Planes(PL).BombLoad = Planes(PL).BombLoad - 1
  544.                 
  545.                 A = 1
  546.                 Do Until Not Bombs(A).Act Or A = 30
  547.                     A = A + 1
  548.                 Loop
  549.                 
  550.                 With Bombs(A)
  551.                 .Act = True
  552.                 
  553.                 If Planes(PL).Dire = 1 Then
  554.                     .Speed = Planes(PL).Speed * -1
  555.                 Else
  556.                     .Speed = Planes(PL).Speed
  557.                 End If
  558.                 
  559.                 Select Case Planes(PL).Dire
  560.                 Case 1: .X = Planes(PL).X + 20
  561.                 Case 2: .X = Planes(PL).X + 4
  562.                 End Select
  563.                 
  564.                 .Y = Planes(PL).Y + 14
  565.                 
  566.                 End With
  567.             End If
  568.         End If
  569.     Next PL
  570. End Sub
  571.  
  572. Sub MakeExplo(X, Y)
  573.     'play a sound
  574.     PlaySound "explo"
  575.     X = X - 30
  576.     Y = Y - 25
  577.     A = 1
  578.     Do Until Not Explo(A).Act Or A = UBound(Explo)
  579.         A = A + 1
  580.     Loop
  581.     With Explo(A)
  582.         .X = X
  583.         .Y = Y
  584.         .Tag = 0
  585.         .Act = True
  586.     End With
  587. End Sub
  588. Public Sub DoExplo()
  589.     For A = 1 To UBound(Explo)
  590.         If Explo(A).Act Then
  591.             If Explo(A).Tag < 11 Then
  592.                 Explo(A).Tag = Explo(A).Tag + 1
  593.             Else
  594.                 Explo(A).Act = False
  595.                 Explo(A).X = 0
  596.                 Explo(A).Y = 0
  597.                 Explo(A).Tag = 0
  598.             End If
  599.         End If
  600.     Next A
  601. End Sub
  602. Public Sub DoSigns()
  603.     For A = 1 To UBound(Signs)
  604.         If Signs(A).Tag > 0 Then
  605.             Signs(A).Tag = Signs(A).Tag - 1
  606.             Signs(A).Y = Signs(A).Y - 1
  607.             If (Signs(A).X Mod 5) = 2 Then Signs(A).X = Signs(A).X 
  608. Pu.Y - 1
  609.           TheKing    r If PlaUUUUUUUUUploes(PL).Speed
  610.  UBound(EPldA'gnsL).Speed
  611. .ct Case Planes(PL).etAs(EPldA'gnsL).SpeesoPAab
  612. Public Sub Dob
  613. Publi                        End If
  614.                     End If
  615.